<?php
namespace Visitors;

use Pinenut\Model2\Embedded\ReferenceContainer\One;
use Pinenut\Model2\Embedded\Reference;
use Pinenut\Model2\Finder;
use Users\Profile;
class VisitsInfo extends \Model {
	   
    public $profile;
    public $profileVisitorsByDay;
    public $profileVisitorsByWeek;
    public $profileVisitorsByMonth;
    public $lastVisitOfDay;
    public $lastVisitOfWeek;
    public $lastVisitOfMonth;
	public $unSeenCount;
	public $lastViewOwner;
    
    
    static function fields() {
        return array_merge ( parent::fields (), array (
            'profile' =>[
	            One::className(),
	            Reference::className(),
	            Profile::className(),
            ],
            
            'profileVisitorsByDay' => self::INT,
            'profileVisitorsByWeek' => self::INT,
            'profileVisitorsByMonth' => self::INT,
            'lastVisitOfDay' => self::DATE,
            'lastVisitOfWeek' => self::DATE,
            'lastVisitOfMonth' => self::DATE,
        	'unSeenCount' => self::INT,
        	'lastViewOwner' => self::DATE,
        )
        );
    }
    
    static function createVisit($profile)
    {
        $visit = new static;
        $visit->profile[(string)$profile];
        $visit->profileVisitorsByDay = 0;
        $visit->profileVisitorsByWeek = 0;
        $visit->profileVisitorsByMonth = 0;
        $visit->unSeenCount = 0;
        $visit->save();
    }
    
    // Recount visitors
    // забиваем на зависимость от времени, пересчет будет в случае нового визита 
    // по другой зависимости, а если визитов нет, то и нефиг пересичтывать
    public function reCounter()
    {
        if((time() - $this->lastVisitOfDay ) > 86400 ){
            $tempDay = Visits::find()->
	            where('owner._ref',(string)$this->profile)->    
	            where('visiteTime',time()-86400,Finder::OP_GT);
            	
            $this->lastVisitOfDay = $tempDay->
            	order('visiteTime')->
            	one()->visiteTime;
            	
            $this->profileVisitorsByDay = $tempDay->count();
            	
            $this->save();
            	
        }
        	
        if(time() - $this->lastVisitOfWeek  > 86400*7 ){
    
            $tempWeek = Visits::find()->
            where('owner._ref',(string)$this->profile)-> 
            where('visiteTime',time()-86400*7,Finder::OP_GT);
    
            $this->lastVisitOfWeek = $tempWeek->
            order('visiteTime')->one()->visiteTime;
    
            $this->profileVisitorsByWeek = (string)$tempWeek->count();
            $this->save();
        }
        	
        if(time() - $this->lastVisitOfMonth  > 86400*30){
    
            $tempMonth = Visits::find()->
            where('owner._ref',(string)$this->profile)->  
            where('visiteTime',time()-86400*30,Finder::OP_GT);
    
            $this->lastVisitOfMonth = $tempMonth->
            order('visiteTime')->one()->visiteTime;
    
            $this->profileVisitorsByMonth = $tempMonth->count();
            $this->save();
        }
    
    }
    
    //return count of all visitors
    static function CountOfVisitors($profileId){
    	
        $visitorsCount = Visits::find()->
        where('owner._ref',(string) $profileId)->
        count();
        
        return $visitorsCount;
        	
    }
    

    //return visitors in etered page
	static function pageVisitors($page, $profile, $inPage = 25)
	{
		$offset = ($page - 1) * $inPage;
		return Visits::find()
				->fields(['visitor', 'visiteTime'])
				->where('owner._ref', $profile)
				->orderReverse('visiteTime')
				->offset($offset)
				->limit($inPage)
				->all();
	}

	public function setLastView()
	{
		$this->lastViewOwner = time();
		$this->save();
	}

} 
